package com.macro.mall.service.impl;

import com.alibaba.druid.sql.visitor.functions.Lcase;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.util.DateUtil;
import com.macro.mall.dao.*;
import com.macro.mall.dto.StoreStatisticsParam;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.service.IndexStatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sun.java2d.pipe.SpanClipRenderer;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class IndexStatisticsServiceImpl implements IndexStatisticsService {

    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Autowired
    private OmsOrderReturnApplyDao omsOrderReturnApplyDao;


    @Autowired
    private UmsWithdrawalApplyMapper umsWithdrawalApplyMapper;

   @Autowired
   private UmsStoreDao umsStoreDao;

    @Autowired
    private UmsMemberMapper umsMemberMapper;

    @Autowired
    private UmsMemberDao umsMemberDao;

    @Autowired
    private UmsStoreMapper umsStoreMapper;
    @Autowired
    private OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;

    @Autowired
    private PmsSkuStockDao pmsSkuStockDao;

    @Autowired
    private OmsOrderDao omsOrderDao;

    @Autowired
    private UmsWithdrawalApplyDao umsWithdrawalApplyDao;



    /**
     * 获取首页待处理统计信息
     * @param admin
     * @param adminType
     * @return
     */
    @Override
    public CommonResult getIndexStatistics(UmsAdminDTO admin, Integer adminType) {
        IndexStatisticsResult indexStatisticsResult = new IndexStatisticsResult();
        try {
            Long storeId = admin.getStoreId();
            if(adminType.equals(2)){
                //查询待付款订单数量
                indexStatisticsResult.setWaitPayCount(omsOrderDao.selectStatisticsByKey(storeId,"status",0));
                //查询待发货订单数量
                indexStatisticsResult.setWaitDeliverCount(omsOrderDao.selectStatisticsByKey(storeId,"status",1));
                //查询待收货订单数量
                indexStatisticsResult.setWaitReceiveCount(omsOrderDao.selectStatisticsByKey(storeId,"status",2));
                //查询已完成订单数量
                indexStatisticsResult.setFinishOrderCount(omsOrderDao.selectStatisticsByKey(storeId,"status",3));
                //查询待处理售后单数量
                indexStatisticsResult.setReturnOrderCount(omsOrderReturnApplyDao.selectReturnOrderCount(storeId,"return_status",0));
                //查询库存预警数量
                indexStatisticsResult.setLowStockCount(pmsSkuStockDao.selectLowStockCount(storeId));
                //查询提现数量
                indexStatisticsResult.setWithdrawalApplyCount(umsWithdrawalApplyDao.selectWithdrawalApplyCount(storeId,0));
            }else if(adminType.equals(1)){
                //省级管理员，可以看到他下面所有的用户的商品列表
                UmsStoreExample example = new UmsStoreExample();
                example.createCriteria().andParentIdEqualTo(admin.getId());
                List<Long> stoIds = this.umsStoreMapper.selectByExample(example).stream().map(UmsStore::getId).collect(Collectors.toList());

                //查询待付款订单数量
                OmsOrderExample omsOrderExample = new OmsOrderExample();
                OmsOrderExample.Criteria criteria = omsOrderExample.createCriteria();
                criteria.andStatusEqualTo(0);
                if(stoIds.size()>0){
                    criteria.andUserStoreIdIn(stoIds);
                }

                List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                indexStatisticsResult.setWaitPayCount(omsOrderList.size());
                omsOrderExample.clear();
                //查询待发货订单数量
                criteria.andStatusEqualTo(1);
                if(stoIds.size()>0){
                    criteria.andUserStoreIdIn(stoIds);
                }


                List<OmsOrder> omsWaitDeliverOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                indexStatisticsResult.setWaitDeliverCount(omsWaitDeliverOrderList.size());
                omsOrderExample.clear();
                //查询待收货订单数量
                criteria.andStatusEqualTo(2);
                if(stoIds.size()>0) {
                    criteria.andUserStoreIdIn(stoIds);
                }
                List<OmsOrder> omsWaitReceiveOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                indexStatisticsResult.setWaitReceiveCount(omsWaitReceiveOrderList.size());
                omsOrderExample.clear();
                //查询已完成订单数量
                criteria.andStatusEqualTo(3);
                if(stoIds.size()>0) {
                    criteria.andUserStoreIdIn(stoIds);
                }
                List<OmsOrder> omsWaitFinishOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                indexStatisticsResult.setFinishOrderCount(omsWaitFinishOrderList.size());
                omsOrderExample.clear();
                //查询待处理售后单数量
                OmsOrderReturnApplyExample omsOrderReturnApplyExample = new OmsOrderReturnApplyExample();
                OmsOrderReturnApplyExample.Criteria criteria1 = omsOrderReturnApplyExample.createCriteria();
                criteria1.andReturnStatusEqualTo(0);
                if(stoIds.size()>0) {
                    criteria1.andStoreIdIn(stoIds);
                }
                List<OmsOrderReturnApply> omsOrderReturnApplyList = omsOrderReturnApplyMapper.selectByExample(omsOrderReturnApplyExample);
                indexStatisticsResult.setReturnOrderCount(omsOrderReturnApplyList.size());
                //查询库存预警数量
                Integer count = 0;
                if(stoIds.size()>0){
                    count =  pmsSkuStockDao.slelectCount(stoIds);
                }
                indexStatisticsResult.setLowStockCount(count);
                //查询提现记录数量
                UmsWithdrawalApplyExample umsWithdrawalApplyExample = new UmsWithdrawalApplyExample();
                UmsWithdrawalApplyExample.Criteria criteria3 = umsWithdrawalApplyExample.createCriteria();
                criteria3.andStatusEqualTo(0);
                if(stoIds.size()>0) {
                    criteria3.andStoreIdIn(stoIds);
                }
                List<UmsWithdrawalApply> umsWithdrawalApplyList = umsWithdrawalApplyMapper.selectByExample(umsWithdrawalApplyExample);
                indexStatisticsResult.setWithdrawalApplyCount(umsWithdrawalApplyList.size());
            }else{
                //查询待付款订单数量
                indexStatisticsResult.setWaitPayCount(omsOrderDao.selectStatisticsByKey(null,"status",0));
                //查询待发货订单数量
                indexStatisticsResult.setWaitDeliverCount(omsOrderDao.selectStatisticsByKey(null,"status",1));
                //查询待收货订单数量
                indexStatisticsResult.setWaitReceiveCount(omsOrderDao.selectStatisticsByKey(null,"status",2));
                //查询已完成订单数量
                indexStatisticsResult.setFinishOrderCount(omsOrderDao.selectStatisticsByKey(null,"status",3));
                //查询待处理售后单数量
                indexStatisticsResult.setReturnOrderCount(omsOrderReturnApplyDao.selectReturnOrderCount(null,"return_status",0));
                //查询库存预警数量
                indexStatisticsResult.setLowStockCount(pmsSkuStockDao.selectLowStockCount(null));
                //查询提现数量
                indexStatisticsResult.setWithdrawalApplyCount(umsWithdrawalApplyDao.selectWithdrawalApplyCount(null,0));
            }


        }catch (Exception e){
            return CommonResult.failed(e.toString());
        }

        return CommonResult.success(indexStatisticsResult);
    }

    /**
     * 查询首页商品统计总览信息
     * @param admin
     * @param adminType
     * @return
     */
    @Override
    public CommonResult indexProductStatistics(UmsAdminDTO admin, Integer adminType) {
        IndexProductStatisticsResult indexProductStatisticsResult = new IndexProductStatisticsResult();
        try {
            Long storeId = admin.getStoreId();
            if(adminType.equals(2)){
                //查询预警商品统计信息
                indexProductStatisticsResult.setLowStockCount(pmsSkuStockDao.selectLowStockCount(storeId));
                //查询下架商品数量
                indexProductStatisticsResult.setUnderCount(pmsSkuStockDao.selectIndexProductStatistics(storeId,0));
                //查询上架商品数量
                indexProductStatisticsResult.setUpCount(pmsSkuStockDao.selectIndexProductStatistics(storeId,1));
                //查询全部商品数量
                indexProductStatisticsResult.setAllCount(pmsSkuStockDao.selectIndexProductStatistics(storeId,null));
            }else if(adminType.equals(1)){
                //省级管理员，可以看到他下面所有的用户的商品列表
                UmsStoreExample example = new UmsStoreExample();
                example.createCriteria().andParentIdEqualTo(admin.getId());
                List<Long> stoIds = this.umsStoreMapper.selectByExample(example).stream().map(UmsStore::getId).collect(Collectors.toList());
                if(stoIds.size()>0){
                    //查询预警商品统计信息
                    indexProductStatisticsResult.setLowStockCount(pmsSkuStockDao.selectLowStordsStockCount(stoIds));
                    //查询下架商品数量
                    indexProductStatisticsResult.setUnderCount(pmsSkuStockDao.selectIndexStordsProductStatistics(stoIds,0));
                    //查询上架商品数量
                    indexProductStatisticsResult.setUpCount(pmsSkuStockDao.selectIndexStordsProductStatistics(stoIds,1));
                    //查询全部商品数量
                    indexProductStatisticsResult.setAllCount(pmsSkuStockDao.selectIndexStordsProductStatistics(stoIds,null));
                }

            }else{
                //查询预警商品统计信息
                indexProductStatisticsResult.setLowStockCount(pmsSkuStockDao.selectLowStockCount(null));
                //查询下架商品数量
                indexProductStatisticsResult.setUnderCount(pmsSkuStockDao.selectIndexProductStatistics(null,0));
                //查询上架商品数量
                indexProductStatisticsResult.setUpCount(pmsSkuStockDao.selectIndexProductStatistics(null,1));
                //查询全部商品数量
                indexProductStatisticsResult.setAllCount(pmsSkuStockDao.selectIndexProductStatistics(null,null));
            }

        }catch (Exception e){
            return CommonResult.failed(e.toString());
        }

        return CommonResult.success(indexProductStatisticsResult);
    }


    /**
     * 查询用户总览统计信息
     */
    @Override
    public CommonResult indexMemberStatistics(UmsAdminDTO admin, Integer adminType) {
        IndexMemberStatisticsResult indexMemberStatisticsResult = new IndexMemberStatisticsResult();
        try {
            Long storeId = admin.getStoreId();
            UmsStoreExample exampleA = new UmsStoreExample();
            exampleA.createCriteria().andParentIdEqualTo(admin.getId());
            List<Long> stoIds = this.umsStoreMapper.selectByExample(exampleA).stream().map(UmsStore::getId).collect(Collectors.toList());

            Date endDate = new Date();
            UmsMemberExample example = new UmsMemberExample();
            if(adminType.equals(2)){
                example.createCriteria().andStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                example.createCriteria().andStoreIdIn(stoIds);
            }
            List<UmsMember> umsMemberList = umsMemberMapper.selectByExample(example);
            //查询全部会员数量
            indexMemberStatisticsResult.setAllCount(umsMemberList.size());//umsMemberMapper.selectIndexMemberStatistics(storeId,null,null)
            //查询当前月份新增会员数
            UmsMemberExample example1 = new UmsMemberExample();
            UmsMemberExample.Criteria criteria1 = example1.createCriteria();
            if(adminType.equals(2)){
                criteria1.andStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria1.andStoreIdIn(stoIds);
            }
            Date beginDate =DateUtil.getFirstDayDateOfMonth(endDate);
            criteria1.andCreateTimeBetween(beginDate,endDate);
            List<UmsMember> umsMemberMonthList = umsMemberMapper.selectByExample(example1);
            indexMemberStatisticsResult.setMonthCount(umsMemberMonthList.size());
            //查询上月会员数量
            Date lastMonthDayF = DateUtil.getLastMonthF();
            Date lastMonthDayE = DateUtil.getLastMonthE();
            UmsMemberExample exampleB = new UmsMemberExample();
            UmsMemberExample.Criteria criteriaB = exampleB.createCriteria();
            if(adminType.equals(2)){
                criteriaB.andStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteriaB.andStoreIdIn(stoIds);
            }
            criteriaB.andCreateTimeBetween(lastMonthDayF,lastMonthDayE);
            List<UmsMember> umsMemberLastMonthList = umsMemberMapper.selectByExample(exampleB);
            Integer umsMemberLastMonthCount = umsMemberLastMonthList.size();
            if(umsMemberLastMonthCount.equals(0)){
                indexMemberStatisticsResult.setLastMonthIncrease(100);
            }else{
                Integer mCount = ((umsMemberMonthList.size()/umsMemberLastMonthCount)-1)*1;
                indexMemberStatisticsResult.setLastMonthIncrease(mCount);
            }
            //查询前一天新增会员
            UmsMemberExample example12 = new UmsMemberExample();
            UmsMemberExample.Criteria criteria2 = example12.createCriteria();
            if(adminType.equals(2)){
                criteria2.andStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria2.andStoreIdIn(stoIds);
            }
            Date yesterdayBeginDate =DateUtil.getFirstYesterday(endDate);
            Date yesterdayEndDate = DateUtil.getEndYesterDay(endDate);
            criteria2.andCreateTimeBetween(yesterdayBeginDate,yesterdayEndDate);
            List<UmsMember> umsMemberYesterdayList = umsMemberMapper.selectByExample(example12);
            indexMemberStatisticsResult.setYesterdayCount(umsMemberYesterdayList.size());
            //查询今天新增会员
            UmsMemberExample example13 = new UmsMemberExample();
            UmsMemberExample.Criteria criteria3 = example13.createCriteria();
            if(adminType.equals(2)){
                criteria3.andStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria3.andStoreIdIn(stoIds);
            }
            Date dayBeginDate =DateUtil.getFirstday(endDate);
            Date dayEndDate = DateUtil.getEndDay(endDate);
            criteria3.andCreateTimeBetween(dayBeginDate,dayEndDate);
            List<UmsMember> umsMemberTodayList = umsMemberMapper.selectByExample(example13);
            indexMemberStatisticsResult.setTodayCount(umsMemberTodayList.size());
            //查询本周新增会员
            Date weekDate = DateUtil.getWeekStartDate();
            UmsMemberExample example2 = new UmsMemberExample();
            UmsMemberExample.Criteria criteria4 = example2.createCriteria();
            if(adminType.equals(2)){
                criteria4.andStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria4.andStoreIdIn(stoIds);
            }
            criteria4.andCreateTimeBetween(weekDate,endDate);
            List<UmsMember> umsMemberWeekList = umsMemberMapper.selectByExample(example2);
            indexMemberStatisticsResult.setWeekCount(umsMemberWeekList.size());
            //查询上周新增会员
            Date lastWeekDate = DateUtil.getLastWeekMonday();
            UmsMemberExample example3 = new UmsMemberExample();
            UmsMemberExample.Criteria criteria5 = example3.createCriteria();
            if(adminType.equals(2)){
                criteria5.andStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria5.andStoreIdIn(stoIds);
            }
            criteria5.andCreateTimeBetween(lastWeekDate,weekDate);
            List<UmsMember> umsMemberLastWeekList = umsMemberMapper.selectByExample(example3);
            Integer umsMemberLastWeekCount = umsMemberLastWeekList.size();
            if(umsMemberLastWeekCount.equals(0)){
                indexMemberStatisticsResult.setLaseWeekIncrease(100.00);
            }else{
                Integer wCount = ((umsMemberWeekList.size()/umsMemberLastWeekCount)-1)*1;
                indexMemberStatisticsResult.setLaseWeekIncrease(wCount.doubleValue());
            }
        }catch (Exception e){
            return CommonResult.failed(e.toString());
        }
        return CommonResult.success(indexMemberStatisticsResult);
    }


    /**
     * 查询订单统计信息
     */
    @Override
    public CommonResult indexOrderStatistics(UmsAdminDTO admin, Integer adminType) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(0);
        statusList.add(1);
        statusList.add(2);
        statusList.add(3);
        IndexOrderStatisticsResult indexOrderStatisticsResult = new IndexOrderStatisticsResult();
        try{
            Long storeId = admin.getStoreId();
            UmsStoreExample exampleA = new UmsStoreExample();
            exampleA.createCriteria().andParentIdEqualTo(admin.getId());
            List<Long> stoIds = this.umsStoreMapper.selectByExample(exampleA).stream().map(UmsStore::getId).collect(Collectors.toList());
            //查询所有订单数和所有销售额
            OmsOrderExample omsOrderExampleA = new OmsOrderExample();
            OmsOrderExample.Criteria criteriaA = omsOrderExampleA.createCriteria();
            criteriaA.andStatusIn(statusList);
            if(adminType.equals(2)){
                criteriaA.andUserStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteriaA.andUserStoreIdIn(stoIds);
            }
            List<OmsOrder> omsOrderAllList = omsOrderMapper.selectByExample(omsOrderExampleA);
            Double sumAllSales = omsOrderAllList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
            indexOrderStatisticsResult.setAllOrderCount(omsOrderAllList.size());
            indexOrderStatisticsResult.setAllPriceIncrease(sumAllSales);
            //查询今日订单数量 和今日销售额
            Date date= new Date();
            Date todayF = DateUtil.getFirstday(date);
            Date todayE = DateUtil.getEndDay(date);
            OmsOrderExample omsOrderExample = new OmsOrderExample();
            omsOrderExample.clear();
            OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
            if(adminType.equals(2)){
                criteria3.andUserStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria3.andUserStoreIdIn(stoIds);
            }

            criteria3.andCreateTimeBetween(todayF,todayE);
            List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
            List<OmsOrder> omsOrderListNotClose = omsOrderList.stream().filter(e -> e.getStatus() > 0 && e.getStatus() < 4).collect(Collectors.toList());
            Double sumToday = 0.0;
            if (!CollectionUtils.isEmpty(omsOrderListNotClose)) {
                sumToday = omsOrderListNotClose.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
            }
            indexOrderStatisticsResult.setTodayOrderCount(omsOrderList.size());
            indexOrderStatisticsResult.setTodayPrice(sumToday);
            //查询昨日订单数量 和昨日销售额
            Date todayYF = DateUtil.getFirstYesterday(date);
            Date todayYE = DateUtil.getEndYesterDay(date);
            OmsOrderExample omsOrderExample1 = new OmsOrderExample();
            OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
            if(adminType.equals(2)){
                criteria1.andUserStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria1.andUserStoreIdIn(stoIds);
            }
            criteria1.andCreateTimeBetween(todayYF,todayYE);
            List<OmsOrder> omsYOrderList = omsOrderMapper.selectByExample(omsOrderExample1);
            List<OmsOrder> omsYOrderListNotClose = omsYOrderList.stream().filter(e -> e.getStatus() > 0 && e.getStatus() < 4).collect(Collectors.toList());
            Double sumYesterday = 0.0;
            if (!CollectionUtils.isEmpty(omsYOrderListNotClose)) {
                sumYesterday = omsYOrderListNotClose.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
            }
            indexOrderStatisticsResult.setYesterdayOrderCount(omsYOrderList.size());
            indexOrderStatisticsResult.setYesterdayPrice(sumYesterday);
            //查看本月订单数 和本月销售额
            Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
            OmsOrderExample omsOrderExample2 = new OmsOrderExample();
            OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
            criteria.andStatusIn(statusList);
            if(adminType.equals(2)){
                criteria.andUserStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria.andUserStoreIdIn(stoIds);
            }
            criteria.andCreateTimeBetween(monthDay,date);
            List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
            Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
            Integer monthCount = omsOrderMonthList.size();
            indexOrderStatisticsResult.setMonthOrderCount(monthCount);
            indexOrderStatisticsResult.setMonthPrice(formatDouble2(sumMonthSales));
            //获得上个月的订单数和销售额
            Date lastMonthDayF = DateUtil.getLastMonthF();
            Date lastMonthDayE = DateUtil.getLastMonthE();
            OmsOrderExample omsOrderExample4 = new OmsOrderExample();
            OmsOrderExample.Criteria criteria4 = omsOrderExample4.createCriteria();
            criteria4.andStatusIn(statusList);
            if(adminType.equals(2)){
                criteria4.andUserStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria4.andUserStoreIdIn(stoIds);
            }
            criteria4.andCreateTimeBetween(lastMonthDayF,lastMonthDayE);
            List<OmsOrder> omsOrderLastMonthList = omsOrderMapper.selectByExample(omsOrderExample4);
            Double sumLastMonthSales = omsOrderLastMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
            Integer lastMonthCount = omsOrderLastMonthList.size();
            //计算月度同比上月增长
            if(sumLastMonthSales.equals(0.00)||lastMonthCount.equals(0)){
                indexOrderStatisticsResult.setMonthPriceIncrease(100.00);
                indexOrderStatisticsResult.setMonthCountIncrease(100.00);
            }else{
                Double mCount = (((monthCount + 0.0) / (lastMonthCount + 0.0))-1.0)*100.0;
                Double mPrice = ((sumMonthSales/sumLastMonthSales)-1.0)*100.0;
                indexOrderStatisticsResult.setMonthCountIncrease(mCount);
                indexOrderStatisticsResult.setMonthPriceIncrease(mPrice);
            }
            //获取本周的订单数和销售额
            Date weekDate = DateUtil.getWeekStartDate();
            OmsOrderExample omsOrderExample5 = new OmsOrderExample();
            OmsOrderExample.Criteria criteria5 = omsOrderExample5.createCriteria();
            criteria5.andStatusIn(statusList);
            if(adminType.equals(2)){
                criteria5.andUserStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria5.andUserStoreIdIn(stoIds);
            }
            criteria5.andCreateTimeBetween(weekDate,date);
            List<OmsOrder> omsOrderWeekList = omsOrderMapper.selectByExample(omsOrderExample5);
            Double sumWeekSales = omsOrderWeekList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
            Integer weekCount = omsOrderWeekList.size();
            indexOrderStatisticsResult.setWeekOrderCount(weekCount);
            indexOrderStatisticsResult.setWeekPrice(sumWeekSales);
            //获取上周的订单数和销售额
            Date lastWeek = DateUtil.getLastWeekMonday();
            OmsOrderExample omsOrderExample6 = new OmsOrderExample();
            OmsOrderExample.Criteria criteria6 = omsOrderExample6.createCriteria();
            criteria6.andStatusIn(statusList);
            if(adminType.equals(2)){
                criteria6.andUserStoreIdEqualTo(storeId);
            }else if(adminType.equals(1)&&stoIds.size()>0){
                criteria6.andUserStoreIdIn(stoIds);
            }
            criteria6.andCreateTimeBetween(lastWeek,weekDate);
            List<OmsOrder> omsOrderLastWeekList = omsOrderMapper.selectByExample(omsOrderExample6);
            Double sumLastWeekSales = omsOrderLastWeekList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
            Integer weekLastCount = omsOrderLastWeekList.size();

            //计算周同比上月增长
            if(sumLastWeekSales.equals(0.00)||weekLastCount.equals(0)){
                indexOrderStatisticsResult.setWeekCountIncrease(100.00);
                indexOrderStatisticsResult.setWeekPriceIncrease(100.00);
            }else{
                Double weekIncreaseCount = (((weekCount + 0.0)/(weekLastCount + 0.0))-1)*100.0;
                Double  weekIncreasePrice = ((sumWeekSales/sumLastWeekSales)-1)*100.0;
                indexOrderStatisticsResult.setWeekCountIncrease(weekIncreaseCount);
                indexOrderStatisticsResult.setWeekPriceIncrease(weekIncreasePrice);
            }

        }catch (Exception e){
            return CommonResult.failed(e.toString());
        }
        return CommonResult.success(indexOrderStatisticsResult);
    }


    /**
     * 店铺统计信息
     * @param admin
     * @param adminType
     * @param storeStatisticsParam
     * @return
     */
    @Override
    public CommonPage<StoreStatisticsResult> storeStatistics(UmsAdminDTO admin, Integer adminType, StoreStatisticsParam storeStatisticsParam) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(1);
        statusList.add(2);
        statusList.add(3);
        PageHelper.startPage(storeStatisticsParam.getPageNum(),storeStatisticsParam.getPageSize());
        UmsStoreExample example = new UmsStoreExample();
        UmsStoreExample.Criteria criteria4 = example.createCriteria();
        if(storeStatisticsParam!=null){
            if(StringUtils.isNotBlank(storeStatisticsParam.getStoreName())){
                criteria4.andStoreNameLike("%"+storeStatisticsParam.getStoreName()+"%");
            }
            if(adminType.equals(1)){
                criteria4.andParentIdEqualTo(admin.getId());
            }
            if(adminType.equals(2)){
                criteria4.andIdEqualTo(admin.getStoreId());
            }
        }
        example.setOrderByClause("id desc");
        List<UmsStore> umsStores = this.umsStoreMapper.selectByExample(example);
        CommonPage<UmsStore> umsStoreCommonPage = CommonPage.restPage(umsStores);
        List<UmsStore> umsStoreList = umsStoreCommonPage.getList();
        List<StoreStatisticsResult> storeStatisticsResultList = new ArrayList<>();
        try{
            Long storeId = admin.getStoreId();
            //如果传入店铺名称
            if(storeStatisticsParam.getStoreName()!=null){
                String storeName = storeStatisticsParam.getStoreName();
                //查询相关店铺
                umsStoreList.forEach(p->{
                    {
                        StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                        Long sId = p.getId();
                        //查询今日销售额
                        Date date = new Date();
                        Date todayF = DateUtil.getFirstday(date);
                        Date todayE = DateUtil.getEndDay(date);
                        OmsOrderExample omsOrderExample = new OmsOrderExample();
                        omsOrderExample.clear();
                        OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                        //可以记入销售额的状态
                        criteria3.andStatusIn(statusList);
                        criteria3.andUserStoreIdEqualTo(sId);
                        criteria3.andCreateTimeBetween(todayF, todayE);
                        List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                        Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setDaySales(sumTodayB);
                        //查看本月订单数 和本月销售额
                        Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                        OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                        OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                        //可以记入销售额的状态
                        criteria.andStatusIn(statusList);
                        criteria.andUserStoreIdEqualTo(sId);
                        if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                            criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                        } else {
                            criteria.andCreateTimeBetween(monthDay, date);
                        }
                        List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                        Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setMonthSales(sumMonthSalesB);
                        //查看入账总额 查看店铺id状态为已完成的订单
                        OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                        OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                        criteria1.andUserStoreIdEqualTo(sId);
                        criteria1.andStatusEqualTo(3);
                        if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                            criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                        }
                        List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                        Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setInSumPrice(inSumPriceB);
                        //查看待入账总额 状态为已发货的订单
                        OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                        OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                        criteria2.andUserStoreIdEqualTo(sId);
                        criteria2.andStatusEqualTo(2);
                        if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                            criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                        }
                        List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                        Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                        //设置店铺名称
                        storeStatisticsResult.setStoreName(p.getStoreName());
                        storeStatisticsResultList.add(storeStatisticsResult);
                    }
                });
                CommonPage<StoreStatisticsResult> result = new CommonPage();
                BeanUtils.copyProperties(umsStoreCommonPage,result);
                result.setList(storeStatisticsResultList);
                return result;
            }
            //如果是门店管理员就只看自己门店的数据
            if(adminType.equals(2)){
                StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                //查询今日销售额
                Date date= new Date();
                Date todayF = DateUtil.getFirstday(date);
                Date todayE = DateUtil.getEndDay(date);
                OmsOrderExample omsOrderExample = new OmsOrderExample();
                omsOrderExample.clear();
                OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                //可以记入销售额的状态
                criteria3.andStatusIn(statusList);
                criteria3.andUserStoreIdEqualTo(storeId);
                criteria3.andCreateTimeBetween(todayF,todayE);
                List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setDaySales(sumTodayB);//formatDouble2(sumToday)
                //查看本月订单数 和本月销售额
                Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                criteria.andUserStoreIdEqualTo(storeId);
//                criteria.andStatusEqualTo(3);
                //可以记入销售额的状态
                criteria.andStatusIn(statusList);
                if(storeStatisticsParam.getStartDate()!=null&&storeStatisticsParam.getEndDate()!=null){
                    criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(),storeStatisticsParam.getEndDate());
                }else{
                    criteria.andCreateTimeBetween(monthDay,date);
                }
                List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setMonthSales(sumMonthSalesB);
                //查看入账总额 查看店铺id状态为已完成的订单
                OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                criteria1.andUserStoreIdEqualTo(storeId);
                criteria1.andStatusEqualTo(3);
                if(storeStatisticsParam.getStartDate()!=null&&storeStatisticsParam.getEndDate()!=null){
                    criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(),storeStatisticsParam.getEndDate());
                }
                List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setInSumPrice(inSumPriceB);
                //查看待入账总额 状态为已发货的订单
                OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                criteria2.andUserStoreIdEqualTo(storeId);
                criteria2.andStatusEqualTo(2);
                if(storeStatisticsParam.getStartDate()!=null&&storeStatisticsParam.getEndDate()!=null){
                    criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(),storeStatisticsParam.getEndDate());
                }
                List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                //设置店铺名称
                UmsStore umsStore= umsStoreMapper.selectByPrimaryKey(storeId);
                storeStatisticsResult.setStoreName(umsStore.getStoreName());
                storeStatisticsResultList.add(storeStatisticsResult);


                CommonPage<StoreStatisticsResult> result = new CommonPage();
                BeanUtils.copyProperties(umsStoreCommonPage,result);
                result.setList(storeStatisticsResultList);
                return result;
            }else if(adminType.equals(1)){
                UmsStoreExample exampleA = new UmsStoreExample();
                exampleA.createCriteria().andParentIdEqualTo(admin.getId());
                List<Long> storeIds = this.umsStoreMapper.selectByExample(exampleA).stream().map(UmsStore::getId).collect(Collectors.toList());
                umsStoreList.forEach(p -> {
                    StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                    //查询今日销售额
                    Date date = new Date();
                    Date todayF = DateUtil.getFirstday(date);
                    Date todayE = DateUtil.getEndDay(date);
                    OmsOrderExample omsOrderExample = new OmsOrderExample();
                    omsOrderExample.clear();
                    OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                    //可以记入销售额的状态
                    criteria3.andStatusIn(statusList);
                    criteria3.andUserStoreIdEqualTo(p.getId());
                    criteria3.andCreateTimeBetween(todayF, todayE);
                    List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                    Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setDaySales(sumTodayB);//formatDouble2(sumToday)
                    //查看本月订单数 和本月销售额
                    Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                    OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                    criteria.andUserStoreIdEqualTo(p.getId());
                    criteria.andStatusEqualTo(3);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    } else {
                        criteria.andCreateTimeBetween(monthDay, date);
                    }
                    List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                    Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setMonthSales(sumMonthSalesB);
                    //查看入账总额 查看店铺id状态为已完成的订单
                    OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                    criteria1.andUserStoreIdEqualTo(p.getId());
                    criteria1.andStatusEqualTo(3);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                    Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setInSumPrice(inSumPriceB);
                    //查看待入账总额 状态为已发货的订单
                    OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                    criteria2.andUserStoreIdEqualTo(p.getId());
                    criteria2.andStatusEqualTo(2);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                    Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                    //设置店铺名称
                    UmsStore umsStore = umsStoreMapper.selectByPrimaryKey(p.getId());
                    storeStatisticsResult.setStoreName(umsStore.getStoreName());
                    storeStatisticsResultList.add(storeStatisticsResult);
                });
            }else{
                List<Long> stordIds =  omsOrderDao.selectStoreId();
                //List<OmsOrder> omsOrderListA = omsOrderMapper.selectByExample(omsOrderExampleA);
                umsStoreList.forEach(p->{
                    StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                    //查询今日销售额
                    Date date = new Date();
                    Date todayF = DateUtil.getFirstday(date);
                    Date todayE = DateUtil.getEndDay(date);
                    OmsOrderExample omsOrderExample = new OmsOrderExample();
                    omsOrderExample.clear();
                    OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                    //可以记入销售额的状态
                    criteria3.andStatusIn(statusList);
                    criteria3.andUserStoreIdEqualTo(p.getId());
                    criteria3.andCreateTimeBetween(todayF, todayE);
                    List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                    Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setDaySales(sumTodayB);//formatDouble2(sumToday)
                    //查看本月订单数 和本月销售额
                    Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                    OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                    //可以记入销售额的状态
                    criteria.andStatusIn(statusList);
                    criteria.andUserStoreIdEqualTo(p.getId());
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    } else {
                        criteria.andCreateTimeBetween(monthDay, date);
                    }
                    List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                    Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setMonthSales(sumMonthSalesB);
                    //查看入账总额 查看店铺id状态为已完成的订单
                    OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                    criteria1.andUserStoreIdEqualTo(p.getId());
                    criteria1.andStatusEqualTo(3);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                    Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setInSumPrice(inSumPriceB);
                    //查看待入账总额 状态为已发货的订单
                    OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                    criteria2.andUserStoreIdEqualTo(p.getId());
                    criteria2.andStatusEqualTo(2);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                    Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                    //设置店铺名称
                    UmsStore umsStore = umsStoreMapper.selectByPrimaryKey(p.getId());
                    storeStatisticsResult.setStoreName(umsStore.getStoreName());
                    storeStatisticsResultList.add(storeStatisticsResult);
                });
            }

        }catch (Exception e){
            //return CommonResult.failed(e.toString());
        }
        CommonPage<StoreStatisticsResult> result = new CommonPage();
        BeanUtils.copyProperties(umsStoreCommonPage,result);
        result.setList(storeStatisticsResultList);
        return result;
    }

    /**
     * 店铺统计信息导出
     * @param admin
     * @param adminType
     * @param storeStatisticsParam
     * @return
     */
    @Override
    public CommonResult storeStatisticsExport(UmsAdminDTO admin, Integer adminType, StoreStatisticsParam storeStatisticsParam) {
        List<Integer> statusList = new ArrayList<>();
        statusList.add(1);
        statusList.add(2);
        statusList.add(3);
        UmsStoreExample example = new UmsStoreExample();
        UmsStoreExample.Criteria criteria4 = example.createCriteria();
        if(storeStatisticsParam!=null){
            if(StringUtils.isNotBlank(storeStatisticsParam.getStoreName())){
                criteria4.andStoreNameLike(storeStatisticsParam.getStoreName());
            }
            if(adminType.equals(1)){
                criteria4.andParentIdEqualTo(admin.getId());
            }
            if(adminType.equals(2)){
                criteria4.andIdEqualTo(admin.getStoreId());
            }
        }
           List<UmsStore> umsStores = this.umsStoreMapper.selectByExample(example);
//        CommonPage<UmsStore> umsStoreCommonPage = CommonPage.restPage(umsStores);
        List<StoreStatisticsResult> storeStatisticsResultList = new ArrayList<>();
        try{
            Long storeId = admin.getStoreId();
            //如果传入店铺名称
            if(storeStatisticsParam.getStoreName()!=null){
                String storeName = storeStatisticsParam.getStoreName();
                //查询相关店铺
                //List<UmsStore> umsStoreList= umsStoreDao.selectBySoreName(storeName);
                //List<UmsStore>  pageStoreList= CommonPage.restPage(umsStoreList).getList();
                umsStores.forEach(p->{
                    {
                        StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                        Long sId = p.getId();
                        //查询今日销售额
                        Date date = new Date();
                        Date todayF = DateUtil.getFirstday(date);
                        Date todayE = DateUtil.getEndDay(date);
                        OmsOrderExample omsOrderExample = new OmsOrderExample();
                        omsOrderExample.clear();
                        OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                        //可以记入销售额的状态
                        criteria3.andStatusIn(statusList);
                        criteria3.andUserStoreIdEqualTo(sId);
                        criteria3.andCreateTimeBetween(todayF, todayE);
                        List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                        Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setDaySales(sumTodayB);//formatDouble2(sumToday)
                        //查看本月订单数 和本月销售额
                        Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                        OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                        OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                        //可以记入销售额的状态
                        criteria.andStatusIn(statusList);
                        criteria.andUserStoreIdEqualTo(sId);
                        if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                            criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                        } else {
                            criteria.andCreateTimeBetween(monthDay, date);
                        }
                        List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                        Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setMonthSales(sumMonthSalesB);
                        //查看入账总额 查看店铺id状态为已完成的订单
                        OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                        OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                        criteria1.andUserStoreIdEqualTo(sId);
                        criteria1.andStatusEqualTo(3);
                        if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                            criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                        }
                        List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                        Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setInSumPrice(inSumPriceB);
                        //查看待入账总额 状态为已发货的订单
                        OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                        OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                        criteria2.andUserStoreIdEqualTo(sId);
                        criteria2.andStatusEqualTo(2);
                        if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                            criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                        }
                        List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                        Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                        BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                        storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                        //设置店铺名称
                        storeStatisticsResult.setStoreName(p.getStoreName());
                        storeStatisticsResultList.add(storeStatisticsResult);
                    }
                });
                return CommonResult.success(storeStatisticsResultList);
            }
            //如果是门店管理员就只看自己门店的数据
            if(adminType.equals(2)){
                StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                //查询今日销售额
                Date date= new Date();
                Date todayF = DateUtil.getFirstday(date);
                Date todayE = DateUtil.getEndDay(date);
                OmsOrderExample omsOrderExample = new OmsOrderExample();
                omsOrderExample.clear();
                OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                //可以记入销售额的状态
                criteria3.andStatusIn(statusList);
                criteria3.andUserStoreIdEqualTo(storeId);
                criteria3.andCreateTimeBetween(todayF,todayE);
                List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setDaySales(sumTodayB);//formatDouble2(sumToday)
                //查看本月订单数 和本月销售额
                Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                //可以记入销售额的状态
                criteria.andStatusIn(statusList);
                criteria.andUserStoreIdEqualTo(storeId);
                if(storeStatisticsParam.getStartDate()!=null&&storeStatisticsParam.getEndDate()!=null){
                    criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(),storeStatisticsParam.getEndDate());
                }else{
                    criteria.andCreateTimeBetween(monthDay,date);
                }
                List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setMonthSales(sumMonthSalesB);
                //查看入账总额 查看店铺id状态为已完成的订单
                OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                criteria1.andUserStoreIdEqualTo(storeId);
                criteria1.andStatusEqualTo(3);
                if(storeStatisticsParam.getStartDate()!=null&&storeStatisticsParam.getEndDate()!=null){
                    criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(),storeStatisticsParam.getEndDate());
                }
                List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setInSumPrice(inSumPriceB);
                //查看待入账总额 状态为已发货的订单
                OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                criteria2.andUserStoreIdEqualTo(storeId);
                criteria2.andStatusEqualTo(2);
                if(storeStatisticsParam.getStartDate()!=null&&storeStatisticsParam.getEndDate()!=null){
                    criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(),storeStatisticsParam.getEndDate());
                }
                List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                //设置店铺名称
                UmsStore umsStore= umsStoreMapper.selectByPrimaryKey(storeId);
                storeStatisticsResult.setStoreName(umsStore.getStoreName());
                storeStatisticsResultList.add(storeStatisticsResult);
                return CommonResult.success(storeStatisticsResultList);
            }else if(adminType.equals(1)){
                UmsStoreExample exampleA = new UmsStoreExample();
                exampleA.createCriteria().andParentIdEqualTo(admin.getId());
                List<Long> storeIds = this.umsStoreMapper.selectByExample(exampleA).stream().map(UmsStore::getId).collect(Collectors.toList());
                umsStores.forEach(p -> {
                    StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                    //查询今日销售额
                    Date date = new Date();
                    Date todayF = DateUtil.getFirstday(date);
                    Date todayE = DateUtil.getEndDay(date);
                    OmsOrderExample omsOrderExample = new OmsOrderExample();
                    omsOrderExample.clear();
                    OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                    //可以记入销售额的状态
                    criteria3.andStatusIn(statusList);
                    criteria3.andUserStoreIdEqualTo(p.getId());
                    criteria3.andCreateTimeBetween(todayF, todayE);
                    List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                    Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setDaySales(sumTodayB);//formatDouble2(sumToday)
                    //查看本月订单数 和本月销售额
                    Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                    OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                    //可以记入销售额的状态
                    criteria.andStatusIn(statusList);
                    criteria.andUserStoreIdEqualTo(p.getId());
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    } else {
                        criteria.andCreateTimeBetween(monthDay, date);
                    }
                    List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                    Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setMonthSales(sumMonthSalesB);
                    //查看入账总额 查看店铺id状态为已完成的订单
                    OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                    criteria1.andUserStoreIdEqualTo(p.getId());
                    criteria1.andStatusEqualTo(3);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                    Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setInSumPrice(inSumPriceB);
                    //查看待入账总额 状态为已发货的订单
                    OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                    criteria2.andUserStoreIdEqualTo(p.getId());
                    criteria2.andStatusEqualTo(2);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                    Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                    //设置店铺名称
                    UmsStore umsStore = umsStoreMapper.selectByPrimaryKey(p.getId());
                    storeStatisticsResult.setStoreName(umsStore.getStoreName());
                    storeStatisticsResultList.add(storeStatisticsResult);
                });
            }else{
                List<Long> stordIds =  omsOrderDao.selectStoreId();
                //List<OmsOrder> omsOrderListA = omsOrderMapper.selectByExample(omsOrderExampleA);
                umsStores.forEach(p->{
                    StoreStatisticsResult storeStatisticsResult = new StoreStatisticsResult();
                    //查询今日销售额
                    Date date = new Date();
                    Date todayF = DateUtil.getFirstday(date);
                    Date todayE = DateUtil.getEndDay(date);
                    OmsOrderExample omsOrderExample = new OmsOrderExample();
                    omsOrderExample.clear();
                    OmsOrderExample.Criteria criteria3 = omsOrderExample.createCriteria();
                    //可以记入销售额的状态
                    criteria3.andStatusIn(statusList);
                    criteria3.andUserStoreIdEqualTo(p.getId());
                    criteria3.andCreateTimeBetween(todayF, todayE);
                    List<OmsOrder> omsOrderList = omsOrderMapper.selectByExample(omsOrderExample);
                    Double sumToday = omsOrderList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumTodayB = new BigDecimal(sumToday).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setDaySales(sumTodayB);//formatDouble2(sumToday)
                    //查看本月订单数 和本月销售额
                    Date monthDay = DateUtil.getFirstDayDateOfMonth(date);
                    OmsOrderExample omsOrderExample2 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria = omsOrderExample2.createCriteria();
                    //可以记入销售额的状态
                    criteria.andStatusIn(statusList);
                    criteria.andUserStoreIdEqualTo(p.getId());
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    } else {
                        criteria.andCreateTimeBetween(monthDay, date);
                    }
                    List<OmsOrder> omsOrderMonthList = omsOrderMapper.selectByExample(omsOrderExample2);
                    Double sumMonthSales = omsOrderMonthList.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal sumMonthSalesB = new BigDecimal(sumMonthSales).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setMonthSales(sumMonthSalesB);
                    //查看入账总额 查看店铺id状态为已完成的订单
                    OmsOrderExample omsOrderExample1 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria1 = omsOrderExample1.createCriteria();
                    criteria1.andUserStoreIdEqualTo(p.getId());
                    criteria1.andStatusEqualTo(3);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria1.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList1 = omsOrderMapper.selectByExample(omsOrderExample1);
                    Double inSumPrice = omsOrderList1.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal inSumPriceB = new BigDecimal(inSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setInSumPrice(inSumPriceB);
                    //查看待入账总额 状态为已发货的订单
                    OmsOrderExample omsOrderExample3 = new OmsOrderExample();
                    OmsOrderExample.Criteria criteria2 = omsOrderExample3.createCriteria();
                    criteria2.andUserStoreIdEqualTo(p.getId());
                    criteria2.andStatusEqualTo(2);
                    if (storeStatisticsParam.getStartDate() != null && storeStatisticsParam.getEndDate() != null) {
                        criteria2.andCreateTimeBetween(storeStatisticsParam.getStartDate(), storeStatisticsParam.getEndDate());
                    }
                    List<OmsOrder> omsOrderList2 = omsOrderMapper.selectByExample(omsOrderExample3);
                    Double waitInSumPrice = omsOrderList2.stream().map(OmsOrder::getPayAmount).map(BigDecimal::toString).map(Double::parseDouble).mapToDouble(d -> d).sum();
                    BigDecimal waitInSumPriceB = new BigDecimal(waitInSumPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
                    storeStatisticsResult.setWaitInSumPrice(waitInSumPriceB);
                    //设置店铺名称
                    UmsStore umsStore = umsStoreMapper.selectByPrimaryKey(p.getId());
                    storeStatisticsResult.setStoreName(umsStore.getStoreName());
                    storeStatisticsResultList.add(storeStatisticsResult);
                });


            }

        }catch (Exception e){
            //return CommonResult.failed(e.toString());
        }
        return CommonResult.success(storeStatisticsResultList);
    }
    /**
     * 查询用户统计折线图
     * @param admin
     * @param adminType
     * @param queryWay
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public CommonResult memberLiceChart(UmsAdminDTO admin, Integer adminType, Integer queryWay, Date startDate, Date endDate) throws ParseException {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>>  memberMap = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<>();
        Date sDate = null;
        Date eDate = null;
        Date dateNow = new Date();
        if(queryWay.equals(0)&&queryWay!=null){
            sDate = DateUtil.getWeekStartDate();
            eDate = DateUtil.getLastWeekDay();
        }else if (queryWay.equals(1)&&queryWay!=null){
            sDate = DateUtil.getFirstDayDateOfMonth(dateNow);
            eDate = DateUtil.lastMonthDay();
        }
        if(startDate!=null && endDate!=null){
            sDate = startDate;
            eDate = endDate;
        }
        Date flag = sDate;
        while (flag.getTime()<=eDate.getTime()){
            dateList.add(sdf.format(flag));
            flag = new Date(flag.getTime()+24*60*60*1000);
        }
        //如果是平台查询全部订单数量
        if(adminType.equals(0)){
            memberMap = umsMemberDao.memberLiceChart(null,queryWay,sDate,eDate);
        }else if(adminType.equals(1)){
            UmsStoreExample exampleA = new UmsStoreExample();
            exampleA.createCriteria().andParentIdEqualTo(admin.getId());
            List<Long> storeIds = this.umsStoreMapper.selectByExample(exampleA).stream().map(UmsStore::getId).collect(Collectors.toList());
            memberMap = umsMemberDao.memberLiceChart(storeIds,queryWay,sDate,eDate);
        }else{
            memberMap = umsMemberDao.memberLiceChartOne(admin.getStoreId(),queryWay,sDate,eDate);
        }
        for (String date:dateList) {
            List<Map<String, Object>> collect = memberMap.stream().filter(e -> e.get("日期") != null && e.get("日期").equals(date)).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect)){
                result.add(collect.get(0));
            }else{
                if(dateNow.before(sdf.parse(date))){
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",date);
                    map.put("数量","");
                    result.add(map);
                }else{
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",date);
                    map.put("数量",0);
                    result.add(map);
                }
            }
        }
//        result.forEach(p->{
//            String s = (String)p.get("日期");
//            if(queryWay.equals(0)&&queryWay!=null){
//                String ss = DateUtil.returnWeek(s);
//                p.put("日期","周"+ss);
//            }else if(queryWay.equals(1)&&queryWay!=null) {
//                p.put("日期",s.substring(8,10));
//            }
//        });
        return CommonResult.success(result);
    }


    /**
     * 查询订单
     * @param admin
     * @param adminType
     * @param queryWay
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public CommonResult orderLiceChart(UmsAdminDTO admin, Integer adminType, Integer queryWay, Date startDate, Date endDate) throws ParseException {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>>  memberMap = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<>();
        Date sDate = null;
        Date eDate = null;
        Date dateNow = new Date();
        if(queryWay.equals(0)&&queryWay!=null){
            sDate = DateUtil.getWeekStartDate();
            eDate = DateUtil.getLastWeekDay();
        }else if (queryWay.equals(1)&&queryWay!=null){
            sDate = DateUtil.getFirstDayDateOfMonth(dateNow);
            eDate = DateUtil.lastMonthDay();
        }
        if(startDate!=null && endDate!=null){
            sDate = startDate;
            eDate = endDate;
        }
        Date flag = sDate;
        while (flag.getTime()<=eDate.getTime()){
            dateList.add(sdf.format(flag));
            flag = new Date(flag.getTime()+24*60*60*1000);
        }
        //如果是平台查询全部订单数量
        if(adminType == null){
            adminType = admin.getAdminType();
        }
        if(adminType.equals(0)){
            memberMap = omsOrderDao.orderLiceChart(null,queryWay,sDate,eDate);
        }else if(adminType.equals(1)){
            UmsStoreExample exampleA = new UmsStoreExample();
            exampleA.createCriteria().andParentIdEqualTo(admin.getId());
            List<Long> storeIds = this.umsStoreMapper.selectByExample(exampleA).stream().map(UmsStore::getId).collect(Collectors.toList());
            memberMap = omsOrderDao.orderLiceChart(storeIds,queryWay,sDate,eDate);
        }else{
            memberMap = omsOrderDao.orderLiceChartOne(admin.getStoreId(),queryWay,sDate,eDate);
        }
        for (String date:dateList) {

            List<Map<String, Object>> collect = memberMap.stream().filter(e -> e.get("日期") != null && e.get("日期").equals(date)).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect)){
                result.add(collect.get(0));
            }else{
                if(dateNow.before(sdf.parse(date))){
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",date);
                    map.put("数量","");
                    result.add(map);
                }else{
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",date);
                    map.put("数量",0);
                    result.add(map);
                }
            }
        }
//        result.forEach(p->{
//            String s = (String)p.get("日期");
//            if(queryWay.equals(0)&&queryWay!=null){
//                String ss = DateUtil.returnWeek(s);
//                p.put("日期","周"+ss);
//            }else if(queryWay.equals(1)&&queryWay!=null) {
//                p.put("日期",s.substring(8,10));
//            }
//        });
        return CommonResult.success(result);
    }


    /**
     * 查询销售额
     * @param admin
     * @param adminType
     * @param queryWay
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public CommonResult salesLiceChart(UmsAdminDTO admin, Integer adminType, Integer queryWay, Date startDate, Date endDate) throws ParseException {
        if (adminType == null) {
            adminType = admin.getAdminType();
        }
        List<Map<String,Object>> result = new ArrayList<>();
        List<Map<String,Object>>  memberMap = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateList = new ArrayList<>();
        Date sDate = null;
        Date eDate = null;
        Date dateNow = new Date();
        if(queryWay.equals(0)&&queryWay!=null){
            sDate = DateUtil.getWeekStartDate();
            eDate = DateUtil.getLastWeekDay();
        }else if (queryWay.equals(1)&&queryWay!=null){
            sDate = DateUtil.getFirstDayDateOfMonth(dateNow);
            eDate = DateUtil.lastMonthDay();
        }
        if(startDate!=null && endDate!=null){
            sDate = startDate;
            eDate = endDate;
        }
        Date flag = sDate;
        while (flag.getTime()<=eDate.getTime()){
            dateList.add(sdf.format(flag));
            flag = new Date(flag.getTime()+24*60*60*1000);
        }
        //如果是平台查询全部订单数量
        if(adminType.equals(0)){
            memberMap = omsOrderDao.orderSalesLiceChart(null,queryWay,sDate,eDate);
        }else if(adminType.equals(1)){
            UmsStoreExample exampleA = new UmsStoreExample();
            exampleA.createCriteria().andParentIdEqualTo(admin.getId());
            List<Long> storeIds = this.umsStoreMapper.selectByExample(exampleA).stream().map(UmsStore::getId).collect(Collectors.toList());
            memberMap = omsOrderDao.orderSalesLiceChart(storeIds,queryWay,sDate,eDate);
        }else{
            memberMap = omsOrderDao.orderSalesLiceChartOne(admin.getStoreId(),queryWay,sDate,eDate);
        }
        for (String date:dateList) {
            List<Map<String, Object>> collect = memberMap.stream().filter(e -> e.get("日期") != null && e.get("日期").equals(date)).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect)){
                result.add(collect.get(0));
            }else{
                if(dateNow.before(sdf.parse(date))){
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",date);
                    map.put("销售额","");
                    result.add(map);
                }else{
                    Map<String,Object> map = new HashMap<>();
                    map.put("日期",date);
                    map.put("销售额",0.00);
                    result.add(map);
                }
            }
        }
//        result.forEach(p->{
//            String s = (String)p.get("日期");
//            if(queryWay.equals(0)&&queryWay!=null){
//                String ss = DateUtil.returnWeek(s);
//                p.put("日期","周"+ss);
//            }else if(queryWay.equals(1)&&queryWay!=null) {
//                p.put("日期",s.substring(8,10));
//            }
//        });
        return CommonResult.success(result);
    }

    public double formatDouble2(double d) {
        // 新方法，如果不需要四舍五入，可以使用RoundingMode.DOWN
//        BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP);
        return (double)Math.round(d*100)/100;
//        return bg.doubleValue();
    }


}
